using System;
using System.Data;
using System.Configuration;

using Expat.Bayesian;
using MySql.Data.MySqlClient;

namespace Grr.Model
{
    public class SpamChecker
    {
        private static SpamFilter filter = new SpamFilter();

        static SpamChecker()
        {
            using (MySqlConnection con = DbEntity.OpenReadConnection ()) {
                DataTable table = new DataTable();
                MySqlDataAdapter adapter = new MySqlDataAdapter ("SELECT IsSpam, Body from SpamFilter", con);
                adapter.Fill(table);

                filter.Load (table);
            }
        }

        public static EntityFlags GetIsSpamFlags(string str)
        {
            double test = filter.Test (str);

            if (test >= filter.Knobs.CertainSpamScore)
                return EntityFlags.CertainlySpam;
            if (test >= filter.Knobs.LikelySpamScore)
                return EntityFlags.ProbablySpam;

            return EntityFlags.ProbablyHam;
        }
    }
}

